<?php
namespace app\admin\controller;

use app\admin\model\Menu;

class Console extends Auth
{

    //如果设置成true,则表示是公共模块，所有操作无需验证
    protected $public_is =true;
    //公共模块，无需权限检测
    //protected $public_action = array('index','top','left','clearcache');

    //后台页面框架
    public function index() {
        self::checkLoginFile();
        $ainfo=db('admin')->where('id','=',UID)->field('id,account')->find();
        $this->assign('ainfo',$ainfo);
        $this->assign('menu_top',$this->top());
        return $this->fetch();
    }


    //后台顶部菜单
    public function top() {
        if(0==ISADMIN)$map[] = ['id','in',get_rules()];

        // 获取一级目录
        $map[] = ['pid','=',0] ;
        $map[] = ['hide_flag','=',0] ;
        $map[] = ['is_show','=',1];
        $menu_top = db('menu')->where($map)->field('id,title,icon')->order('sort desc,id desc')->select();
        return $menu_top;
    }


    //后台左侧菜单
    public function left() {
        $top_key = input('top',1) ;
        if(!$top_key)return false;
        //读取缓存权限节点，减少数据库查询
        $result=$this->menu_left_tree($top_key);
        if($result){
            echo $result;
            return;
        }

        $map=[];
        if(0==ISADMIN)$map[] = ['id','in',get_rules()];
        $object=new Menu();
        $menu_tree = $object->getTree($map);
        $res = $menu_tree[$top_key]['child'] ;

        $html = "" ;
        $_node_num=0;  //左侧菜单列数量（包括父级列）
        $_max_num=10;   //允许最大的数量 (0不限)
        $set_icon='/static/common/img/img/852.png';  //默认图标
        $default_url=get_admin_login_file();
        foreach($res as $value){
            $_hover_style=' on';
            if($_max_num>0 && $_max_num<=$_node_num){
                $_hover_state='style="display:none"';
                $_hover_style='';
            }

            $html .= "<h3 class='f14'><span class='J_switchs cu".$_hover_style."' title='展开或关闭'></span><img src='".($value['icon']?$value['icon']:$set_icon)."'>".$value['title']."</h3>" ;
            $_node_num++;
            $html .= "<ul ".$_hover_state.">" ;
            foreach($value['child'] as $val){
                $html .= "<li class='sub_menu'>" ;
                $html .= "<a href='javascript:;' data-uri='/{$default_url}/".$val['path']."' data-id='".$val['id']."' hidefocus='true'><img src='".($val['icon']?$val['icon']:$set_icon)."'/>".$val['title']."</a>" ;
                $html .= "</li>" ;
                $_node_num++;
            }
            $html .= "</ul>" ;
        }

        //保存缓存权限节点，减少数据库查询
        $this->menu_left_tree($top_key,$html);

        echo $html ;
    }


    /**
     * 保存\读取右侧菜单数据,减少数据库读取，提升效率
     * @param $top         一级菜单id
     * @param bool $html   菜单html , 不设置则读取，设置则保存
     */
    private function menu_left_tree($top,$html=false){
        if(!$top)return false;
        $menu_left_tree=session('menu_left_tree');
        if(!$menu_left_tree && !$html)return false;
        $menu_rtn = unserialize($menu_left_tree);
        if(!$html && $menu_rtn[$top])return $menu_rtn[$top];
        $menu_rtn[$top]=$html;
        session('menu_left_tree',serialize($menu_rtn));
    }


    //检查后台登录地址
    private function checkLoginFile(){
        $res=$_SERVER['SCRIPT_NAME'];
        if($res!='/'.get_admin_login_file())return $this->redirect('/');
    }


    //清除系统缓存
    public function clearcache(){
        $save_pre = config("common.admin_login_save_pre");
        session($save_pre.'_rules',null);  //当前账号拥有的权限ids
        session($save_pre.'_author',null); //当前账号已验证的权限
        session('__forward__',null);
        session('menu_left_tree',null);
    }

}
